﻿using System;
using System.Collections.Generic;

using HL.Lib.MVC;
using HL.Lib.Models;

namespace HL.Lib.Controllers
{
    public class CFilterController : Controller
    {
        //[HL.Core.MVC.PropertyInfo("Chuyên mục", "Type|Product")]
        public int MenuID = 0;

        [HL.Core.MVC.PropertyInfo("Tiêu đề")]
        public string Title;

        public override void OnLoad()
        {
            if (ViewPage.CurrentPage.MenuID > 0)
                MenuID = ViewPage.CurrentPage.MenuID;

            int PropertyID = 0;
            if (MenuID > 0)
            {
                var menu = WebMenuService.Instance.GetByID(MenuID);
                PropertyID = menu.PropertyID;
                while (true)
                {
                    if (PropertyID > 0)
                        break;

                    menu = WebMenuService.Instance.GetByID(menu.ParentID);

                    if (menu == null)
                        break;

                    PropertyID = menu.PropertyID;

                    if (menu.ParentID == 0)
                        break;
                }
                if (PropertyID > 0)
                {
                    var p = ModPropertyService.Instance.GetByID(PropertyID);
                    //if (p.ParentID == 0)
                    //{
                    //    var pc = ModPropertyService.Instance.CreateQuery()
                    //        .Where(o => o.ParentID == p.ID)
                    //        .OrderByAsc(o => o.ID)
                    //        .ToSingle();
                    //    if (pc != null) PropertyID = pc.ID;
                    //}
                    //if (p.ParentID > 0)
                    //{
                        var query = ModPropertyService.Instance.CreateQuery()
                            .Where(o => o.ParentID == PropertyID)
                            .OrderByAsc(o=>o.Order)
                            .ToList();
                        ViewBag.Data = query;
                    //}
                }
            }
            ViewBag.Title = Title;
        }
    }
}
